package com.subway.babynote;

/*水*/
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import com.subway.babynote.heightinput.ItemClickListener;
import com.subway.babynote.syncdata.ClientDataRecord.uxRecord;
import com.subway.babynote.syncservice.syncapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.view.KeyEvent;
import android.view.View.OnClickListener;
import android.database.Cursor;

public class gridviewinputamount extends Activity {
	private static final int INVISIBLE = 0x00000004;
	static final int RG_AMOUNT = 6;
	TextView unit_view;
	EditText amount_et;
	TextView food_view;
	TextView hint_view;
	GridView gridview;
	NotedbHelper notedb;
	Button login_button;
	Button change_button;
	List<GridInfo> list;
	GridAdapter adapter;
	Cursor cur;
	int col;
	int food_id;
	String category;
	Bundle bundle;
	private LinearLayout linearLayout, statistics, sync, more;
	Intent In;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.inputgridview);
		setTitle(R.string.note);
		In = new Intent();
		// initTabFunction();
		food_view = (TextView) findViewById(R.id.noted_view);
		unit_view = (TextView) findViewById(R.id.unit_view);
		hint_view = (TextView) findViewById(R.id.hint_view);
		amount_et = (EditText) findViewById(R.id.amount_edit);
		amount_et.setFocusable(true);
		amount_et.setInputType(InputType.TYPE_CLASS_NUMBER);
        unit_view.setVisibility(View.GONE);
        amount_et.setVisibility(View.GONE);
		food_id = bundle.getInt("food_id");
		List<Float> l = syncapi.getSyncImpl().getFoodSuggestion(food_id).l;
		Iterator<Float> it=l.iterator();
		gridview = (GridView) findViewById(R.id.gridview);
		list = new ArrayList<GridInfo>();
		while(it.hasNext()){
		  list.add(new GridInfo(it.next().toString()));
		
		}
		list.add(new GridInfo("other"));
		adapter = new GridAdapter(this);  
		adapter.setList(list);  
		gridview.setAdapter(adapter);  
		gridview.setOnItemClickListener(new ItemClickListener());
		
		change_button = (Button) findViewById(R.id.change_button);
		login_button = (Button) findViewById(R.id.login_button);
		bundle = this.getIntent().getExtras();
	//	notedb = new NotedbHelper(this);
		
		category = bundle.getString("category");
		try {
			if (syncapi.getSyncImpl().getUser().isuser) {
				login_button.setVisibility(INVISIBLE);
				login_button.setClickable(false);
			}
		//	cur = notedb.getFood(food_id);
/*
			col = cur.getColumnIndex("unit");
			unit_view.setText(cur.getString(col));
			cur.close(); */
		} catch (Exception e) {
			Log.v("inputamount", "get info from DB e=" + e.getMessage());
		}
		food_view.setText(bundle.getString("foodname"));
		if (!bundle.getString("foodname").matches(
				".*" + getResources().getStringArray(R.array.food_category)[1]
						+ ".*")) {
			change_button.setVisibility(INVISIBLE);
			change_button.setClickable(false);
		}
		// unit_view.setText("ml");
		amount_et.setOnKeyListener(new EditText.OnKeyListener() {
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				// TODO Auto-generated method stub
				Log.v("babynote", "keycode=" + keyCode);
				switch (keyCode) {
				case KeyEvent.KEYCODE_ENTER:
					Log.v("BabyNote", "amount confirm ...");

					bundle.putInt("food_id", food_id);
					Float qt = Float.parseFloat(amount_et.getText().toString());
					if (qt > 300) {

						hint_view.setText(getResources().getString(
								R.string.invalidrange));
						return false;
					} else {
						hint_view.setText("");
					}
					bundle.putFloat("quantity",
							Float.parseFloat(amount_et.getText().toString()));

					bundle.putString("amount", amount_et.getText().toString());
					bundle.putString("unit", unit_view.getText().toString());
					bundle.putString("foodname", food_view.getText().toString());

					In.setClass(gridviewinputamount.this, inputtime.class);
					In.putExtras(bundle);
					startActivityForResult(In, RG_AMOUNT);

					// startActivity(In);
					return true;
				}
				return false;
			}
		});

		change_button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				bundle.putInt("food_id", food_id);
				bundle.putString("from", "inputamount");
				bundle.putString("category", category);
				In.setClass(gridviewinputamount.this, choosebrand.class);
				In.putExtras(bundle);
				startActivity(In);
			}
		});
		login_button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

				In.setClass(gridviewinputamount.this, login.class);
				startActivity(In);
			}
		});
	}
	
	class ItemClickListener implements OnItemClickListener {
		public void onItemClick(AdapterView<?> arg0,// The AdapterView where the
													// click happened
				View arg1,// The view within the AdapterView that was clicked
				int arg2,// The position of the view in the adapter
				long arg3// The row id of the item that was clicked
		) {
			// 在本例中arg2=arg3
			HashMap<String, Object> item = (HashMap<String, Object>) arg0
					.getItemAtPosition(arg2);
			// 显示所选Item的ItemText
			String itemtext = (String) item.get("ItemText");
			if (itemtext.equals("other")) {
				 unit_view.setVisibility(View.VISIBLE);
			     amount_et.setVisibility(View.VISIBLE);

			} else {
				try {
				
					bundle.putFloat("quantity",Float.parseFloat(itemtext));

				} catch (NumberFormatException nfe) {
					System.out.println("NumberFormatException: "
							+ nfe.getMessage());
				}
			}

		}
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		setResult(RESULT_OK);
		this.finish();
		Log.v("inputamoutn", "on activty result");

	}

	public void onDestroy() {
		notedb.close();
		super.onDestroy();

	}
}